//
//  ViewController.swift
//  UIView及子类构造函数
//
//  Created by lantaiyuan on 2017/8/19.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    //初始化label,并且分配空间,比super.viewDidLoad()早
    //移动端开发  '延迟加载' 减少内存消耗
    //懒加载---- 延迟创建   最大的好处---解除解包的烦恼
    //lazy var label : DemoLabel = DemoLabel()
    
    //懒加载本身是一个闭包--完整写法
    /*
      {}  包装代码
      () 执行代码
     */
    
    //这种完整写法会出现 循环引用
//    lazy var label: DemoLabel = {
//        let i = DemoLabel()
//        i.text = "hahahahah"
//        i.sizeToFit()
//        return i
//    }()
//    lazy var label = { ()->DemoLabel in
//        let l = DemoLabel()
//        l.text = "正常点好不好"
//        return l
//    }()
    
    private lazy var label : UILabel? = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupUI()
        
        
        
        
        //释放label
//        label = nil
//        //一旦label设置为nil,懒加载不会在执行，懒加载只会在第一次调用懒加载的时候调用,执行闭包,然后将闭包的结果保存在label的属性中
//        
//        print(label as AnyObject)
    }
    //私有的
    private func setupUI() {

        //会调用懒加载
        print(label as AnyObject)
        label?.text = "label"
        label?.sizeToFit()
        label?.center = view.center
        view.addSubview(label!)
        
        print(label as AnyObject)
        
        label?.isUserInteractionEnabled = true
        label?.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(labelGesAction(_:))))
    }
    func labelGesAction(_ ges : UITapGestureRecognizer) {
        let vc = CalculatedPropertiesController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

